package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.commom.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //判断当前用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();

        //返回结果为map
        Map<String,Object> map = new HashMap<>();

        if(user == null){
            map.put("islike",false);
            map.put("isfollow",false);
            map.put("isunlike",false);
            map.put("iscollection",false);
            return ResponseResult.okResult(map);
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));

        //默认全设置为false
        map.put("islike", false);
        map.put("isunlike",false);
        map.put("iscollection",false);
        map.put("isfollow",false);
        //根据行为实体查询是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (apLikesBehavior != null) {
            map.put("islike", true);
        }
        //根据行为实体查询是否不喜欢
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if(unlikesBehavior != null) {
            map.put("isunlike",true);
        }
        //查询是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if(apCollection != null){
            map.put("iscollection",true);
        }
        //查询是否为此作者的粉丝
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + apBehaviorEntry.getRefId(), String.valueOf(dto.getAuthorApUserId()));
        if(score != null){
            map.put("isfollow",true);
        }
        //封装结果 返回
        return ResponseResult.okResult(map);
    }
}
